package com.jchy.renderit.displayable;
 
import com.jchy.renderit.core.Dir;
import com.jchy.renderit.core.Displayable;
import com.jchy.renderit.core.Hit;
import com.jchy.renderit.core.Point;

public class Plane extends Displayable
{

    public Plane()
    {
    }    

    public Hit intersect(Point prp, Point vrp)
    {
        Hit aHit = new Hit();        
        Point utprp, utvrp;
        Dir   utd;

        Point utloc = new Point();
        Point utnormpt = new Point();

        utprp = untransform(prp);
        utvrp = untransform(vrp);
        utd   = utprp.toDir(utvrp);
        utd.normalize();

        double t;

        // Intersect ray starting at untransformed Projection Reference Point, 
        // direction towards untransformed View Reference Point with 
        // xy plane where z=0.0
        double denom=utd.dz;
        if(denom!=0.0) t=-(utprp.z)/denom;
        else t=-1.0;

        if(t>0.00001)
        { 
            aHit.isHit=true;
            aHit.color=new java.awt.Color(
                getPrimaryColorRed(), 
                getPrimaryColorGreen(), 
                getPrimaryColorBlue());

            utloc.x=utprp.x+t*utd.dx;
            utloc.y=utprp.y+t*utd.dy;
            utloc.z=utprp.z+t*utd.dz;
            utnormpt.x=utloc.x;
            utnormpt.y=utloc.y;
            utnormpt.z=utloc.z+1.0;
            aHit.location=transform(utloc);
            aHit.normpt=transform(utnormpt);
            aHit.ambient=ambient;
            aHit.diffuse=diffuse;
            aHit.phongCoef=phongCoef;
            aHit.phongExp=phongExp;

        }
        else aHit.isHit=false;

        return aHit;        
    }

}

